package com.hankol.feature_wine_meal.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.transition.AutoTransition;
import androidx.transition.TransitionManager;

import com.baselibrary.Constants;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ARouterUtils;
import com.baselibrary.utils.GlideUtils;
import com.baselibrary.utils.MarginUtils;
import com.baselibrary.utils.ScreenUtils;
import com.baselibrary.view.RoundCornerImageView;
import com.hankol.feature_wine_meal.R;
import com.hankol.feature_wine_meal.bean.CharacteristicBannerBean;
import com.hankol.feature_wine_meal.bean.ViewCoordinate;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: 关忆北、
 * @date: 2022/6/10
 * @Des：
 */
public class HourHandBanner extends RelativeLayout {
    private static final String TAG = "HourHandBanner";
    private RoundCornerImageView mIvOne, mIvTwo, mIvThree;
    private CardView mCdOne, mCdTwo, mCdThree;
    private TextView mTvOneName, mTvOneDes, mTvTwoName, mTvTwoDes, mTvThreeName, mTvThreeDes;
    public static final long executeTime = 1000;
    private ArrayList<ViewCoordinate> arrayList = new ArrayList<>();
    private ViewGroup transitionsContainer;
    private int count = 0;
    private String drinkFoodPlateName = "";
    private int bigWidth = 0;
    private int smallHeight = 0;
    private final Handler tHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            startTranslateAnimation();
            tHandler.sendEmptyMessageDelayed(0, 1000 * 3);
            return false;
        }
    });
    private ArrayList<CharacteristicBannerBean> characteristicBeans;

    public HourHandBanner(Context context) {
        super(context);
        initView();
    }

    public HourHandBanner(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HourHandBanner(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {

        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.hour_hand_banner, this);
        transitionsContainer = (ViewGroup) inflate.findViewById(R.id.transitions_container);
        mIvOne = (RoundCornerImageView) transitionsContainer.findViewById(R.id.iv_one);
        mIvTwo = (RoundCornerImageView) transitionsContainer.findViewById(R.id.iv_two);
        mIvThree = (RoundCornerImageView) transitionsContainer.findViewById(R.id.iv_three);

        mCdOne = (CardView) transitionsContainer.findViewById(R.id.cd_one);
        mCdTwo = (CardView) transitionsContainer.findViewById(R.id.cd_two);
        mCdThree = (CardView) transitionsContainer.findViewById(R.id.cd_three);

        MarginUtils.INSTANCE.setViewGroupParamsOfWineMealBannerHeight(transitionsContainer.findViewById(R.id.rl_root), 226f / (226 + 110), 340f / 226f);
        MarginUtils.INSTANCE.setViewGroupParamsOfWineMealBanner(mCdOne, 226f / (226 + 110), 340f / 226f);
        MarginUtils.INSTANCE.setViewGroupParamsOfWineMealBanner(mCdTwo, 110f / (226 + 110), 165f / 110f);
        MarginUtils.INSTANCE.setViewGroupParamsOfWineMealBanner(mCdThree, 110f / (226 + 110), 165f / 110f);

        bigWidth = mCdOne.getLayoutParams().width;
        smallHeight = mCdTwo.getLayoutParams().height;

        mTvOneName = (TextView) findViewById(R.id.tv_one_name);
        mTvOneDes = (TextView) findViewById(R.id.tv_one_des);
        mTvOneName.getPaint().setFakeBoldText(true);

        mTvTwoName = (TextView) findViewById(R.id.tv_two_name);
        mTvTwoDes = (TextView) findViewById(R.id.tv_two_des);
        mTvTwoName.getPaint().setFakeBoldText(true);

        mTvThreeName = (TextView) findViewById(R.id.tv_three_name);
        mTvThreeDes = (TextView) findViewById(R.id.tv_three_des);
        mTvThreeName.getPaint().setFakeBoldText(true);

        mCdOne.setTag(1);
        mCdTwo.setTag(2);
        mCdThree.setTag(3);

        initListener();
        tHandler.sendEmptyMessageDelayed(0, 1000 * 2);
    }

    public void setData(ArrayList<CharacteristicBannerBean> bean, String drinkFoodPlateName) {
        characteristicBeans = bean;
        this.drinkFoodPlateName = drinkFoodPlateName;
        if (bean != null) {
            if (bean.size() >= 1) {
                CharacteristicBannerBean characteristicBannerBean = bean.get(0);
                mTvOneName.setText(characteristicBannerBean.getName());
                mTvOneDes.setText(characteristicBannerBean.getDesc());
                GlideUtils.loadImg_228(getContext().getApplicationContext(), characteristicBannerBean.getCover(), mIvOne);
            }
            if (bean.size() >= 2) {
                CharacteristicBannerBean characteristicBannerBean = bean.get(1);
                mTvTwoName.setText(characteristicBannerBean.getName());
                mTvTwoDes.setText(characteristicBannerBean.getDesc());
                GlideUtils.loadImg_228(getContext().getApplicationContext(), bean.get(1).getCover(), mIvTwo);
            }
            if (bean.size() >= 3) {
                CharacteristicBannerBean characteristicBannerBean = bean.get(2);
                mTvThreeName.setText(characteristicBannerBean.getName());
                mTvThreeDes.setText(characteristicBannerBean.getDesc());
                GlideUtils.loadImg_228(getContext().getApplicationContext(), bean.get(2).getCover(), mIvThree);
            }
        }

    }

    @SuppressLint("Recycle")
    public void startTranslateAnimation() {
        if (count >= 3) count = 0;
        count++;
        if (arrayList.size() < 3) {
            arrayList.add(getCoordinate(mCdOne));
            arrayList.add(getCoordinate(mCdTwo));
            arrayList.add(getCoordinate(mCdThree));
        }

        upViewCoordinate(mCdOne);
        upViewCoordinate(mCdTwo);
        upViewCoordinate(mCdThree);
    }

    private ViewCoordinate getCoordinate(View view) {
        ViewCoordinate viewCoordinate = new ViewCoordinate();
        viewCoordinate.setViewX(view.getX());
        viewCoordinate.setViewY(view.getY());
        viewCoordinate.setViewWidth(view.getWidth());
        viewCoordinate.setViewHeight(view.getHeight());
        return viewCoordinate;
    }


    private void upViewCoordinate(View view) {
        AnimatorSet(view);
    }

    public void initListener() {
        mCdOne.setOnClickListener(view -> {
            if (characteristicBeans != null && characteristicBeans.size() >= 1) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("DRINK_FOOD_PLATE_NAME", "特色专区");
                hashMap.put("CLASS_NAME", drinkFoodPlateName);
                hashMap.put("RECOMMEND_NAME", "热门精选");
                hashMap.put(Constants.WINE_MEAL_DETAILS_ID, characteristicBeans.get(0).getId());
                ARouterUtils.jump(ActivityRouter.WINE_MEAL.WINE_MEAL_DETAILS, hashMap);

            }
        });
        mCdTwo.setOnClickListener(view -> {
            if (characteristicBeans != null && characteristicBeans.size() >= 2) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("DRINK_FOOD_PLATE_NAME", "特色专区");
                hashMap.put("CLASS_NAME", drinkFoodPlateName);
                hashMap.put("RECOMMEND_NAME", "热门精选");
                hashMap.put(Constants.WINE_MEAL_DETAILS_ID, characteristicBeans.get(1).getId());
                ARouterUtils.jump(ActivityRouter.WINE_MEAL.WINE_MEAL_DETAILS, hashMap);

            }
        });
        mCdThree.setOnClickListener(view -> {
            if (characteristicBeans != null && characteristicBeans.size() >= 3) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("DRINK_FOOD_PLATE_NAME", "特色专区");
                hashMap.put("CLASS_NAME", drinkFoodPlateName);
                hashMap.put("RECOMMEND_NAME", "热门精选");
                hashMap.put(Constants.WINE_MEAL_DETAILS_ID, characteristicBeans.get(2).getId());
                ARouterUtils.jump(ActivityRouter.WINE_MEAL.WINE_MEAL_DETAILS, hashMap);

            }
        });
    }


    private void AnimatorSet(View view) {
        view.setPivotX(0);
        view.setPivotY(0);
        if (view.getId() == R.id.cd_one) {
            switch (count) {
                case 1:
                    //变化到右下
                    final AnimatorSet animatorSet = new AnimatorSet();
                    animatorSet.playTogether(
                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(0).getViewX(), arrayList.get(1).getViewX()),
                            ObjectAnimator.ofFloat(view, "translationY", arrayList.get(0).getViewY(), arrayList.get(1).getViewY()),
                            ObjectAnimator.ofFloat(view, "scaleX", 1, 110f / 226f),
                            ObjectAnimator.ofFloat(view, "scaleY", 1, 110f / 226f)
                    );

                    animatorSet.setDuration(executeTime);
                    animatorSet.addListener(new AnimatorListener().setListener(new AnimatorListener.OnAnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            beginDelayedTransition(transitionsContainer);
                        }

                        @Override
                        public void onAnimationEnd() {
                            mTvOneName.setTextSize(16 * (226f / 110f));
                            mTvOneDes.setTextSize(14 * (226f / 110f));
                            mTvOneName.setVisibility(VISIBLE);
                            mTvOneDes.setVisibility(GONE);
                            mCdOne.setRadius(ScreenUtils.dpToPx(8 * (226f / 110f)));
                        }
                    }));
                    animatorSet.start();
                    break;
                case 2:
                    //变化到右上
                    final AnimatorSet animatorSet2 = new AnimatorSet();
                    animatorSet2.playTogether(
//                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(1).getViewX(), arrayList.get(2).getViewX()),
                            ObjectAnimator.ofFloat(view, "translationY", arrayList.get(1).getViewY(), arrayList.get(2).getViewY()));
                    animatorSet2.setDuration(executeTime);
                    animatorSet2.start();
                    break;
                case 3:
                    //变化到左大
                    final AnimatorSet animatorSet3 = new AnimatorSet();
                    animatorSet3.playTogether(
                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(2).getViewX(), arrayList.get(0).getViewX()),
                            ObjectAnimator.ofFloat(view, "translationY", arrayList.get(2).getViewY(), arrayList.get(0).getViewY()),
                            ObjectAnimator.ofFloat(view, "scaleX", 110f / 226f, 1),
                            ObjectAnimator.ofFloat(view, "scaleY", 110f / 226f, 1)
                    );
                    animatorSet3.setDuration(executeTime);
                    animatorSet3.addListener(new AnimatorListener().setListener(new AnimatorListener.OnAnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            beginDelayedTransition(transitionsContainer);
                            mTvOneName.setTextSize(16);
                            mTvOneDes.setTextSize(14);
                            mTvOneName.setVisibility(VISIBLE);
                            mTvOneDes.setVisibility(VISIBLE);
                            mCdOne.setRadius(ScreenUtils.dpToPx(8));

                        }

                        @Override
                        public void onAnimationEnd() {

                        }
                    }));
                    animatorSet3.start();
                    break;
            }
        } else if (view.getId() == R.id.cd_two) {
            switch (count) {
                case 1:
                    //变化到右上
                    final AnimatorSet animatorSet = new AnimatorSet();

                    animatorSet.playTogether(
                            ObjectAnimator.ofFloat(view, "translationY", 0, arrayList.get(2).getViewY() - arrayList.get(1).getViewY()));
                    animatorSet.setDuration(executeTime);
                    animatorSet.start();
                    break;
                case 2:
                    //变化到左大
                    final AnimatorSet animatorSet2 = new AnimatorSet();
                    animatorSet2.playTogether(
                            ObjectAnimator.ofFloat(view, "scaleX", 1, 226f / 110f),
                            ObjectAnimator.ofFloat(view, "scaleY", 1, 226f / 110f),
                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(2).getViewX() - arrayList.get(1).getViewX(), arrayList.get(0).getViewX() - arrayList.get(2).getViewX())

                    );
                    animatorSet2.setDuration(executeTime);
                    animatorSet2.addListener(new AnimatorListener().setListener(new AnimatorListener.OnAnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            beginDelayedTransition(transitionsContainer);
                            mTvTwoName.setTextSize(16 / (226f / 110f));
                            mTvTwoDes.setTextSize(14 / (226f / 110f));
                            mTvTwoName.setVisibility(VISIBLE);
                            mTvTwoDes.setVisibility(VISIBLE);
                            mCdTwo.setRadius(ScreenUtils.dpToPx(8 / (226f / 110f)));
                        }

                        @Override
                        public void onAnimationEnd() {

                        }
                    }));
                    animatorSet2.start();
                    break;

                case 3:
                    //变化到右下
                    final AnimatorSet animatorSet3 = new AnimatorSet();
                    animatorSet3.playTogether(
                            ObjectAnimator.ofFloat(view, "scaleX", 226f / 110f, 1),
                            ObjectAnimator.ofFloat(view, "scaleY", 226f / 110f, 1),
                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(0).getViewX() - arrayList.get(2).getViewX(),
                                    arrayList.get(1).getViewX() - arrayList.get(0).getViewX() - ScreenUtils.dip2px(getContext(), 11) - bigWidth),
                            ObjectAnimator.ofFloat(view, "translationY", arrayList.get(2).getViewY() - arrayList.get(1).getViewY(),
                                    arrayList.get(1).getViewY() - arrayList.get(0).getViewY() - ScreenUtils.dip2px(getContext(), 10) - smallHeight)
                    );
                    animatorSet3.setDuration(executeTime);
                    animatorSet3.addListener(new AnimatorListener().setListener(new AnimatorListener.OnAnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            beginDelayedTransition(transitionsContainer);
                            mCdTwo.setRadius(ScreenUtils.dpToPx(8));
                        }

                        @Override
                        public void onAnimationEnd() {
                            mTvTwoName.setTextSize(16);
                            mTvTwoDes.setTextSize(14);
                            mTvTwoName.setVisibility(VISIBLE);
                            mTvTwoDes.setVisibility(GONE);
                        }
                    }));
                    animatorSet3.start();
                    break;
            }
        } else if (view.getId() == R.id.cd_three) {
            switch (count) {
                case 1:
                    //变化到左大
                    final AnimatorSet animatorSet = new AnimatorSet();
                    animatorSet.playTogether(
                            ObjectAnimator.ofFloat(view, "scaleX", 1, 226f / 110f),
                            ObjectAnimator.ofFloat(view, "scaleY", 1, 226f / 110f),
                            ObjectAnimator.ofFloat(view, "translationX", 0, arrayList.get(0).getViewX() - arrayList.get(2).getViewX())
                    );
                    animatorSet.setDuration(executeTime);
                    animatorSet.addListener(new AnimatorListener().setListener(new AnimatorListener.OnAnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            beginDelayedTransition(transitionsContainer);
                            mTvThreeName.setTextSize(16 / (226f / 110f));
                            mTvThreeDes.setTextSize(14 / (226f / 110f));
                            mTvThreeName.setVisibility(VISIBLE);
                            mTvThreeDes.setVisibility(VISIBLE);
                            mCdThree.setRadius(ScreenUtils.dpToPx(8 / (226f / 110f)));
                        }

                        @Override
                        public void onAnimationEnd() {

                        }
                    }));
                    animatorSet.start();
                    break;
                case 2:
                    //变化到右下
                    final AnimatorSet animatorSet2 = new AnimatorSet();
                    animatorSet2.playTogether(
                            ObjectAnimator.ofFloat(view, "scaleX", 226f / 110f, 1),
                            ObjectAnimator.ofFloat(view, "scaleY", 226f / 110f, 1),
                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(0).getViewX() - arrayList.get(2).getViewX(),
                                    arrayList.get(1).getViewX() - arrayList.get(0).getViewX() - ScreenUtils.dip2px(getContext(), 11) - bigWidth),
                            ObjectAnimator.ofFloat(view, "translationY", arrayList.get(2).getViewY() - arrayList.get(0).getViewY(),
                                    arrayList.get(1).getViewY() - arrayList.get(0).getViewY()));
                    animatorSet2.setDuration(executeTime);
                    animatorSet2.addListener(new AnimatorListener().setListener(new AnimatorListener.OnAnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            beginDelayedTransition(transitionsContainer);
                            mCdThree.setRadius(ScreenUtils.dpToPx(8));
                        }

                        @Override
                        public void onAnimationEnd() {
                            mTvThreeName.setTextSize(16);
                            mTvThreeDes.setTextSize(14);
                            mTvThreeName.setVisibility(VISIBLE);
                            mTvThreeDes.setVisibility(GONE);
                        }
                    }));
                    animatorSet2.start();
                    break;

                case 3:
                    //变化到右上，即原来的位置
                    final AnimatorSet animatorSet3 = new AnimatorSet();
                    animatorSet3.playTogether(
                            ObjectAnimator.ofFloat(view, "translationX", arrayList.get(1).getViewX() - arrayList.get(0).getViewX()
                                    - ScreenUtils.dip2px(getContext(), 11) - bigWidth, arrayList.get(2).getViewX() - ScreenUtils.dip2px(getContext(), 11) - bigWidth),
                            ObjectAnimator.ofFloat(view, "translationY", arrayList.get(1).getViewY() -
                                    arrayList.get(0).getViewY(), arrayList.get(2).getViewY()));
                    animatorSet3.setDuration(executeTime);
                    animatorSet3.start();
                    break;
            }
        }
    }

    /**
     * 开始动画
     */
    private void beginDelayedTransition(ViewGroup view) {
        AutoTransition autoTransition = new AutoTransition();
        autoTransition.setDuration(executeTime / 2);
        TransitionManager.beginDelayedTransition(view, autoTransition);
    }

    private static class AnimatorListener implements Animator.AnimatorListener {
        private OnAnimatorListener listener;
        private int count = 0;

        public AnimatorListener setListener(OnAnimatorListener listener) {
            this.listener = listener;
            return this;
        }

        @Override
        public void onAnimationStart(Animator animator) {
            count += 1;
            if (count == 1) {
                //执行想要执行的操作..//因为原声的onAnimationStart会回执两次
                if (listener != null) listener.onAnimationStart();
            }
        }

        @Override
        public void onAnimationEnd(Animator animator) {
            count = 0;
            if (listener != null) listener.onAnimationEnd();
        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }

        interface OnAnimatorListener {
            void onAnimationStart();

            void onAnimationEnd();
        }
    }
}
